<?php


namespace app\controller\machine;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;


/**
 * 智能终端
 */
class Index extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 验证设备信息
     */
	public function api_machine_index_firstConnect($msg)
    {   
        $info = [
           'code' => isset($msg['data']['code'])?$msg['data']['code']:'',
           'details' => isset($msg['data']['details'])?$msg['data']['details']:'',
           'latitude' => isset($msg['data']['latitude'])?$msg['data']['latitude']:'',
           'longitude' => isset($msg['data']['longitude'])?$msg['data']['longitude']:'',
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        $machine = $this->table->find('equipment_list',['code'=>$info['code']]);
        if(empty($machine)){
            return $this->outMessage(-1,'该设备不存在');
        }
        $update = [
            'status' => 2,
            'details' => $info['details'],
            'latitude' => $info['latitude'],
            'longitude' => $info['longitude'],
            'update_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->update('equipment_list',['code'=>$info['code']],$update);
        if(!$res){
            return $this->outMessage(-1,'验证失败');
        }
        //存储信息
        $_SESSION['_EQUIPMENT_CODE_'.$info['code']] = $machine;
        //添加开机设备日志
        $log = [
            'equipment_name' => $machine['name'],
            'equipment_code' => $info['code'],
            'type' => 1,
            'ip' => $_SERVER['REMOTE_ADDR'],
            'linkman' => $machine['linkman'],
            'linkman_mobile' => $machine['linkman_mobile'],
            'remark' => '设备主动开机',
            'merchant_id' => $machine['merchant_id'],
            'merchant_name' => $machine['merchant_name'],
            'region_id' => $machine['region_id'],
            'region_name' => $machine['region_name'],
            'itoc_id' => $machine['itoc_id'],
            'itoc_name' => $machine['itoc_name'],
            'create_time' => date('Y-m-d H:i:s',time())
        ];
        $this->table->insert('equipment_log',$log);
        $data['linkman'] = $machine['linkman'];
        $data['linkman_mobile'] = $machine['linkman_mobile'];
        $data['versions'] = $machine['versions'];
		return $this->outMessage(10000,'验证成功',$data);
	}




    /**
     * 验证商品列表
     */
    public function api_machine_index_getList($msg)
    {   
        $code = isset($msg['data']['code'])?$msg['data']['code']:'';
        if(empty($code)){
            return $this->outMessage(-1,'设备编号不能为空');
        }
        @$machine = $_SESSION['_EQUIPMENT_CODE_'.$code];
        if(empty($machine) || !isset($machine)){
            return $this->outMessage(-1,'请先进行验证');
        }
        $goods_list = $this->table->select('equipment_goods',['equipment_code'=>$code]);
        if(!empty($goods_list)){
            foreach ($goods_list as $k => $v) {
                unset($goods_list[$k]['equipment_id'],$goods_list[$k]['equipment_name'],$goods_list[$k]['is_delete'],$goods_list[$k]['update_time']);
            }
        }
        return $this->outMessage(10000,'验证成功',$goods_list);
    }



    /**
     * 添加设备故障记录
     */
    public function api_machine_index_malfunctionLog($msg)
    {
        $code = isset($msg['data']['code'])?$msg['data']['code']:'';
        $type = isset($msg['data']['type'])?$msg['data']['type']:'';
        $describe = isset($msg['data']['describe'])?$msg['data']['describe']:'';
        if(!$code || !$type){
            return $this->outMessage(-1,'参数不能为空');
        }
        @$machine = $_SESSION['_EQUIPMENT_CODE_'.$code];
        if(empty($machine) || !isset($machine)){
            return $this->outMessage(-1,'请先进行验证');
        }
        $data = [
            'equipment_id' => $machine['id'],
            'equipment_code' => $code,
            'equipment_name' => $machine['name'],
            'ip' => $_SERVER['REMOTE_ADDR'],
            'linkman' => $machine['linkman'],
            'linkman_mobile' => $machine['linkman_mobile'],
            'type' => $type,
            'describe' => $describe,
            'merchant_id' => $machine['merchant_id'],
            'merchant_name' => $machine['merchant_name'],
            'region_id' => $machine['region_id'],
            'region_name' => $machine['region_name'],
            'status' => 1,
            'malfunction_time' => date('Y-m-d H:i:s',time()),
            'create_time' => date('Y-m-d H:i:s',time()),
        ];
        $res = $this->table->insert('equipment_malfunction',$data);
        if(!$res){
            return $this->outMessage(-1,'添加失败');
        }
        return $this->outMessage(10000,'添加成功');
    }



    /**
     * 获取轨道商品信息
     */
    public function api_machine_index_getPathwayList($msg)
    {
        $code = isset($msg['data']['code'])?$msg['data']['code']:'';
        if(empty($code)){
            return $this->outMessage(-1,'设备编号不能为空');
        }
        @$machine = $_SESSION['_EQUIPMENT_CODE_'.$code];
        if(empty($machine) || !isset($machine)){
            return $this->outMessage(-1,'请先进行验证');
        }
        $goods_list = $this->table->select('equipment_goods',['equipment_code'=>$code]);
        if(!empty($goods_list)){
            foreach ($goods_list as $k => $v) {
                unset($goods_list[$k]['equipment_id'],$goods_list[$k]['equipment_name'],$goods_list[$k]['is_delete'],$goods_list[$k]['update_time']);
            }
        }
        return $this->outMessage(10000,'验证成功',$goods_list);
    }



    /**
     * 编辑轨道商品信息
     */
    public function api_machine_index_pathwayEdit($msg)
    {
        $code = isset($msg['data']['code'])?$msg['data']['code']:'';
        $pathway_list = isset($msg['data']['pathway_list'])?$msg['data']['pathway_list']:'';
        if(empty($code)){
            return $this->outMessage(-1,'设备编号不能为空');
        }
        @$machine = $_SESSION['_EQUIPMENT_CODE_'.$code];
        if(empty($machine) || !isset($machine)){
            return $this->outMessage(-1,'请先进行验证');
        }
        if(empty($pathway_list) || !is_array($pathway_list)){
            return $this->outMessage(-1,'轨道编辑信息不能为空');
        }
        try {
            foreach ($pathway_list as $v) {
                if(!empty($v['pathway_id'])){
                    $goods = $this->table->find('equipment_goods',['equipment_code'=>$code,'pathway_id'=>$v['pathway_id']]);
                    if(empty($goods)){
                        return $this->outMessage(-1,'该轨道信息不存在');
                    }
                    if(!empty($v['goods_name'])){
                        $data['goods_name'] = $v['goods_name'];
                    }
                    if(!empty($v['price'])){
                        $data['price'] = $v['price'];
                    }
                    if(!empty($v['number'])){
                        $data['surplus_number'] = $goods['surplus_number']+$v['number'];
                    }
                    if(!empty($data)){
                        $res = $this->table->update('equipment_goods',['equipment_code'=>$code,'pathway_id'=>$v['pathway_id']],$data);
                    }
                }
            }
        } catch (Exception $e) {
            return $this->outMessage(-1,'编辑失败');
        }
        return $this->outMessage(10000,'编辑成功');
    }


}
